package com.xuecheng.content.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.UserVo;
import com.xuecheng.content.config.SmsTemplate;
import com.xuecheng.content.service.Commonservice;
import com.xuecheng.system.api.SystemClient;
import freemarker.template.utility.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.redis.core.RedisTemplate;
import java.time.Duration;

@Service
public class CommonserviceImpl implements Commonservice {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private SystemClient systemClient;

    //发送短信
    @Override
    public ResponseResult smsMsg(String phone) {

        String sj=phone;
        //如果为空,就获取当前登录人
        if (StrUtil.isEmpty(sj)) {

            ResponseResult<UserVo> userVoResponseResult = systemClient.phone();
            UserVo userVo = userVoResponseResult.getData();
            sj= userVo.getPhone();
        }
        // 1.生成六位随机数
        String code = RandomUtil.randomNumbers(6);
        code = "123"; // TODO 开发期间写死
        // 2.调用阿里云sms发短信
        //smsTemplate.sendSms(phone, code);   // TODO 开发期间注释
        // 3.将验证码存入redis （5分钟）
        redisTemplate.opsForValue().set("code:"+sj,code , Duration.ofSeconds(300) );
        return ResponseResult.okResult(true);
    }
}
